package com.ctgu.tengyun.controller.filetemplate;

import com.ctgu.tengyun.bean.convert.FileConvert;
import com.ctgu.tengyun.bean.convert.FileTemplateConvert;
import com.ctgu.tengyun.bean.response.FileTemplateListResponse;
import com.ctgu.tengyun.domain.FileTemplate;
import com.ctgu.tengyun.domain.TemplateType;
import com.ctgu.tengyun.service.filetemplate.FileTemplateService;
import com.ctgu.tengyun.util.ParameterTool;
import com.ctgu.tengyun.util.StringUtils;
import com.ctgu.tengyun.util.Validator;
import com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/8/10.
 */
@RestController
@RequestMapping(value = "/v1/file_template/")
public class FileTemplateController {

    @Resource
    private FileTemplateService fileTemplateService;

    @PostMapping(value = "save_or_update")
    public Object saveOrUpdate(@RequestBody FileTemplate request) {
        Validator.assertHasFields(!StringUtils.isEmpty(request.getName()), "template name is required.");
        Validator.assertHasFields(null != request.getTemplateType(), "templateType is required.");
        Validator.assertHasFields(null != request.getTemplateFile(), "templateFile is required.");
        FileConvert.splitFileBeanUrlPath(request.getTemplateFile());

        if (null != request.getId()) {
            FileTemplate fileTemplate = fileTemplateService.selectByPrimaryKey(request.getId());
            Validator.assertResourceFound(null != fileTemplate, "fileTemplate not found, template id:" + request.getId());

            fileTemplateService.updateByPrimaryKeySelective(request);
        } else {
            fileTemplateService.insertSelective(request);
        }

        return ImmutableMap.of("id", request.getId());
    }

    @RequestMapping(value = "list")
    public FileTemplateListResponse list(HttpServletRequest request) {
        Integer pageNo = ParameterTool.getParameterInteger(request, "pageNo", 1);
        Integer pageSize = ParameterTool.getParameterInteger(request, "pageSize", 10);
        Integer templateType = ParameterTool.getParameterInteger(request, "templateType", null);

        Validator.validateGt(pageNo, 0);
        Validator.validateBetweenIn(pageSize, 0, 1000);

        if (null != templateType) {
            TemplateType.valueOf(templateType);
        }

        Integer totalCount = fileTemplateService.countFileTemplate(templateType);
        List<FileTemplate> templateList = Collections.emptyList();
        if (totalCount > 0) {
            templateList = fileTemplateService.selectFileTemplateList(templateType, pageNo, pageSize);
        }

        return FileTemplateConvert.toFileTemplateListResponse(totalCount, pageSize, templateList);
    }

    @GetMapping(value = "detail/{id}")
    public FileTemplate detail(@PathVariable("id") Integer templateId) {
        FileTemplate fileTemplate = fileTemplateService.selectByPrimaryKey(templateId);
        Validator.assertResourceFound(null != fileTemplate, "template file not found, templateId:" + templateId);

        return FileTemplateConvert.fillFileTemplate(fileTemplate);
    }
}
